package fr.jgraph.business.display;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import fr.jgraph.model.edge.DirectedEdge;
import fr.jgraph.model.edge.Edge;
import fr.jgraph.model.graph.old.AbstractGraph;
import fr.jgraph.model.vertex.old.Vertex;

public class TextGraphDisplayer {
	public String display(AbstractGraph graph) {
		StringBuffer buffer = new StringBuffer();
		
		buffer.append(graph.getName() + "=(");

		// Vertices
		buffer.append("{");
		Iterator itVert = graph.getVertices().iterator();
		while (itVert.hasNext()) {
			Vertex vertex = (Vertex) itVert.next();
			buffer.append(vertex.toString());
			if (itVert.hasNext())
				buffer.append(",");
		}
		buffer.append("},\r\n\t");
		
		// Edges
		buffer.append("{");
		Iterator itEdges = graph.getEdges().iterator();
		while (itEdges.hasNext()) {
			Edge edge = (Edge) itEdges.next();
			buffer.append(display(edge));
			if (itEdges.hasNext())
				buffer.append(",");
		}
		buffer.append("}");
		
		buffer.append(")");
		
		return buffer.toString();
	}
	
	public String display(Edge edge) {
		Iterator itExtr = Arrays.asList(edge.vertexSet()).iterator();

		StringBuffer buffer = new StringBuffer("(");

		while (itExtr.hasNext()) {
			Vertex v = (Vertex) itExtr.next();
			buffer.append(v.toString());
			if (itExtr.hasNext())
				buffer.append(",");
		}
		buffer.append(")");
		
		return buffer.toString();
	}
	
	public String display(DirectedEdge edge) {
		return "(" + display(edge.getSource()) + "," + display(edge.getTarget()) + ")";
	}
	
	public String display(Vertex vertex) {
		return vertex.toString();
	}
}
